package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

 */
@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据商品分类id，查询规格组的集合
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupListByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(this.specGroupService.queryGroupListByCategoryId(cid));
    }


    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroup(@RequestParam("id") Long id) {
        return ResponseEntity.ok(this.specGroupService.listSpecGroup(id));
    }

    /**
     * 查询规格参数集合
     *
     * @param cid 分类id
     * @param gid    规格组id
     * @return 参数集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecparams(
            @RequestParam(value = "categoryId", required = false) Long cid,
            @RequestParam(value = "groupId", required = false) Long gid
    ) {
        return ResponseEntity.ok(specParamService.querySpecparams(cid, gid));
    }


    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        this.specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }


    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        this.specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        this.specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.ok().build();
    }


    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        this.specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.ok().build();
    }
}
